package com.bestcem.xm.ticket.dao.converter;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.ticket.entity.mongo.BaseEntity;
import com.bestcem.xm.ticket.entity.mysql.BaseDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * DO和DTO基础属性转换类
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 16:55
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CommonConverter {

    public static <E extends BaseEntity, D extends BaseDO> void setDo(E entity, D dataObj) {
        dataObj.setRecordFlag(entity.getRecordFlag());
        dataObj.setCreatorId(entity.getCreatedId());
        dataObj.setOperatorId(entity.getUpdatedId());
        dataObj.setCreateTime(entity.getCreatedDate());
        dataObj.setUpdateTime(entity.getUpdatedDate());
    }

    public static <E extends BaseEntity, D extends BaseDO> void setEntity(E entity, D dataObj) {
        entity.setRecordFlag(dataObj.getRecordFlag());
        entity.setCreatedId(dataObj.getCreatorId());
        entity.setUpdatedId(dataObj.getOperatorId());
        entity.setCreatedDate(dataObj.getCreateTime());
        entity.setUpdatedDate(dataObj.getUpdateTime());
    }

    public static <T> List<T> stringToList(String string, Class<T> clazz) {
        return JSON.parseArray(string, clazz);
    }

    public static <T> T stringToObject(String string, Class<T> clazz) {
        return JSON.parseObject(string, clazz);
    }

    public static <T> String listToString(List<T> list) {
        if (!CollectionUtils.isEmpty(list)) {
            return JSON.toJSONString(list);
        }
        return null;
    }

    public static String objectToString(Object obj) {
        if (Objects.nonNull(obj)) {
            return JSON.toJSONString(obj);
        }
        return null;
    }
}
